<?php
namespace app\index\controller;

use Db;
use think\Controller;

class Index extends Controller
{
    /**
     * 首页
     * @return mixed
     */
    public function index()
    {

        //热门电影
        $hotMovies = Db::name('movies')->field('id,title,poster,actors,grade,resolution')
            ->where('pid < 1 AND status=1 AND play_type=1')
            ->order('is_hot desc,id desc')->limit(12)->select();

        $this->assign('hotMovies', $hotMovies);

        //电影类型
        $types = Db::name('movie_type')->where('pid', 0)->order('rank desc')->limit(8)->select();
        $this->assign('types', $types);

        //电影地区
        $regions = Db::name('region')->order('rank desc')->limit(4)->select();
        $this->assign('regions', $regions);


        //电影排行榜
        $tops = Db::name('movies')->field('id,title,create_time')
            ->where('status=1 AND play_type=1')->order('grade desc,movie_time desc')->limit(12)->select();
        $this->assign('tops', $tops);


        //最新电影
        $newMovies = Db::name('movies')->field('id,title,poster,actors,grade,resolution')
            ->where('status=1 AND play_type=1')
            ->order('movie_time desc')->limit(10)->select();
        $this->assign('newMovies', $newMovies);


        return $this->fetch();
    }


    /**
     * 详情页
     */
    public function details()
    {

        $id = input('movie_id', 0);

        //影片信息
        $info = Db::name('movies a')->field('a.*,b.region_name,c.type_name')
            ->leftJoin('region b', 'a.region_id=b.id')
            ->leftJoin('movie_type c', 'a.movie_type=c.id')
            ->where('a.id', $id)->find();
        $this->assign('info', $info);

        $type = !empty($info['movie_type']) ? $info['movie_type'] : 0;

        //当前分类排行榜
        $tops = Db::name('movies')->field('id,title,grade')
            ->where('status=1 AND play_type=1 AND movie_type=' . $type)->order('grade desc,movie_time desc')->limit(20)->select();
        $this->assign('tops', $tops);

        //相关电影
        $movies = Db::name('movies')->field('id,title,poster,actors,grade,resolution')
            ->where('id <>' . $id . ' AND status=1 AND play_type=1 AND movie_type=' . $type)
            ->order('movie_time desc')->limit(10)->select();

        $movies2 = Db::name('movies')->field('id,title,poster,actors,grade,resolution')
            ->where('id <>' . $id . ' AND status=1 AND play_type=1 AND movie_type=' . $type)
            ->order('movie_time desc')->limit(10, 10)->select();

        $this->assign('movies', $movies);
        $this->assign('movies2', $movies2);

        return $this->fetch();
    }


    /**
     * 播放页
     */
    public function play()
    {


        $id = input('movie_id', 0);

        //影片信息
        $info = Db::name('movies a')->field('a.*,b.region_name,c.type_name')
            ->leftJoin('region b', 'a.region_id=b.id')
            ->leftJoin('movie_type c', 'a.movie_type=c.id')
            ->where('a.id', $id)->find();
        $this->assign('info', $info);


        if($info){

            //记录播放记录
            $record = cookie('play_record');

            $key = 'm'.$id;

            $arr = [$key=>$info['title']];

            if($record){

                $record = json_decode($record,true);

                if(!empty($record[$key])){
                    unset($record[$key]);
                }

                if(count($record)>=20){

                    array_pop($record);
                }

                $arr = array_merge($arr,$record);
            }

            \Cookie::forever('play_record',json_encode($arr,JSON_UNESCAPED_UNICODE));

            if(empty($record[$key])){
                Db::name('movies')->where('id',$id)->setInc('pv');

            }
        }


        $type = !empty($info['movie_type']) ? $info['movie_type'] : 0;

        //当前分类排行榜
        $tops = Db::name('movies')->field('id,title,grade')
            ->where('status=1 AND play_type=1 AND movie_type=' . $type)->order('grade desc,movie_time desc')->limit(15)->select();
        $this->assign('tops', $tops);

        //相关电影
        $movies = Db::name('movies')->field('id,title,poster,actors,grade,resolution')
            ->where('id <>' . $id . ' AND status=1 AND play_type=1 AND movie_type=' . $type)
            ->order('movie_time desc')->limit(10)->select();

        $movies2 = Db::name('movies')->field('id,title,poster,actors,grade,resolution')
            ->where('id <>' . $id . ' AND status=1 AND play_type=1 AND movie_type=' . $type)
            ->order('movie_time desc')->limit(10, 10)->select();

        $this->assign('movies', $movies);
        $this->assign('movies2', $movies2);

        return $this->fetch();
    }



    /*
     * 今日更新
     */
    public function todayUpdate(){
        $movies = Db::name('movies')->field('id,title,poster,actors,grade,resolution')
            ->where('status=1 AND create_time>='.time())
            ->order('id desc')->limit(60)->select();

        $this->assign('movies',$movies);
        return $this->fetch();
    }

    /*
     * 电影列表
     */
    public function movie(){



        $where = [['status','=',1],['play_type','=',1]];

        $search = input('get.');

        if(!empty($search['movie_type'])){
            //类型
            $where[] = ['movie_type','=' , $search['movie_type']];
        }

        if(!empty($search['region_id'])){
            //地区
            $where[] = ['region_id','=' , $search['region_id']];
        }

        if(!empty($search['movie_year'])){
            //上映时间

            if($search['movie_year'] == 'more'){
                $where[] = ['movie_year','<=' , $search['movie_year']];

            }else{
                $where[] = ['movie_year','=' , $search['movie_year']];

            }
        }

//        dump($where);

        $order = '';

        if(!empty($search['rank'])){

            switch($search['rank']){
                case  'pv':
                    $order.='pv desc,';
                    break;
                case 'hot':
                    $order.='is_hot desc,is_recommend desc';
                    break;
            }
        }

        $order = $order.'movie_time desc';
        //电影列表
        $movies = Db::name('movies')->field('id,title,poster,actors,grade,resolution,movie_year')
            ->where($where)
            ->order($order)->paginate(36,false,['query'=>request()->param()]);

        $this->assign('movies',$movies);

        //总记录数
        $total = Db::name('movies')->where($where)->count();
        $this->assign('total',$total);


        //电影类型
        $types = Db::name('movie_type')->where('pid', 0)->order('rank desc')->limit(20)->select();
        $this->assign('types', $types);

        //电影地区
        $regions = Db::name('region')->order('rank desc')->limit(20)->select();
        $this->assign('regions', $regions);

        //年份
        $years = range(date('Y'),date('Y')-10);
        $this->assign('years', $years);


        return $this->fetch();
    }


    public function search(){

        $text = input('keyword');
        $this->assign('text',$text);


        //电影列表
        $movies = Db::name('movies a')->field('c.type_name,a.language,a.movie_time,a.id,a.title,a.poster,a.actors,a.directors,a.grade,a.resolution,a.movie_year,b.region_name')
            ->leftJoin('region b','b.id=a.region_id')
            ->leftJoin('movie_type c','c.id=a.movie_type')
            ->where('a.title|a.actors|a.directors|b.region_name','like',"%$text%")
            ->order('a.movie_time desc')->paginate(10,false,['query'=>request()->param()]);

        $this->assign('movies',$movies);


        //总记录条数
        $total = Db::name('movies')->where('title|actors|directors','like',"%$text%")->count();
        $this->assign('total',$total);


        //电影排行榜
        $tops = Db::name('movies')->field('id,title,create_time,grade')
            ->where('status=1 AND play_type=1')->order('grade desc,movie_time desc')->limit(15)->select();
        $this->assign('tops', $tops);

        return $this->fetch();
    }
}
